home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / PROG_TOO / C027B.ZIP / TOP / MAIN.C < prev    next >
Text File  |  1990-03-30  |  3KB  |  147 lines

  1. /* Copyright (c) 1988 by Sozobon, Limited.  Author: Tony Andrews
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  */
  11. #include "top.h"
  12.  
  13. FILE    *ifp, *ofp;        /* input/output file pointers */
  14.  
  15. long    _STKSIZ = 32768L;    /* need mucho stack for recursion */
  16.  
  17. /*
  18.  * Options 
  19.  */
  20. bool    debug   = FALSE;
  21. bool    do_brev = TRUE;        /* branch reversals enabled */
  22. bool    do_peep = TRUE;        /* peephole optimizations enabled */
  23. bool    do_dflow= TRUE;        /* enable data-flow analysis */
  24. bool    verbose = FALSE;
  25.  
  26. /*
  27.  * Optimization statistics (use -v to print)
  28.  */
  29. int    s_bdel = 0;        /* branches deleted */
  30. int    s_badd = 0;        /* branches added */
  31. int    s_brev = 0;        /* branch reversals */
  32. int    s_peep1 = 0;        /* 1 instruction peephole changes */
  33. int    s_peep2 = 0;        /* 2 instruction peephole changes */
  34. int    s_peep3 = 0;        /* 3 instruction peephole changes */
  35. int    s_idel = 0;        /* instructions deleted */
  36.  
  37. #define    TMPFILE    "top_tmp.$$$"    /* temporary file name */
  38. int    use_temp = FALSE;    /* using temporary file */
  39.  
  40. char    *Version =
  41. "top Version 1.01  Copyright (c) 1988 by Sozobon, Limited.";
  42.  
  43. usage()
  44. {
  45.     fprintf(stderr, "usage: top [-dvfbp] infile [outfile]\n");
  46.     exit(1);
  47. }
  48.  
  49. main(argc, argv)
  50. int    argc;
  51. char    *argv[];
  52. {
  53.     FILE    *fopen();
  54.     register char    *s;
  55.  
  56.     while (argc > 1 && argv[1][0] == '-') {
  57.         for (s = &argv[1][1]; *s ;s++) {
  58.             switch (*s) {
  59.             case 'd':
  60.                 debug = TRUE;
  61.                 break;
  62.             case 'b':
  63.                 do_brev = FALSE;
  64.                 break;
  65.             case 'p':
  66.                 do_peep = FALSE;
  67.                 break;
  68.             case 'f':
  69.                 do_dflow = FALSE;
  70.                 break;
  71.             case 'v':
  72.                 fprintf(stderr, "%s\n", Version);
  73.                 verbose = TRUE;
  74.                 break;
  75.             default:
  76.                 usage();
  77.                 break;
  78.             }
  79.         }
  80.         argv++;
  81.         argc--;
  82.     }
  83.  
  84.     if (argc > 3)
  85.         usage();
  86.  
  87.     if (argc > 1) {
  88.         if (strcmp(argv[1], "-") == 0)
  89.             ifp = stdin;
  90.         else if ((ifp = fopen(argv[1], "r")) == NULL) {
  91.             fprintf(stderr, "top: can't open input file '%s'\n",
  92.                 argv[1]);
  93.             exit(1);
  94.         }
  95.         if (argc > 2) {
  96.             if (strcmp(argv[2], "-") == 0)
  97.                 ofp = stdout;
  98.             else if ((ofp = fopen(argv[2], "w")) == NULL) {
  99.                 fprintf(stderr, "top: can't open output file '%s'\n",
  100.                     argv[2]);
  101.                 exit(1);
  102.             }
  103.         } else {
  104.             if ((ofp = fopen(TMPFILE, "w")) == NULL) {
  105.                 fprintf(stderr, "top: can't create temp file\n");
  106.                 exit(1);
  107.             }
  108.             use_temp = TRUE;
  109.         }
  110.     } else
  111.         usage();
  112.  
  113.     dofile();
  114.  
  115.     if (verbose) {
  116.         if (do_peep) {
  117.             fprintf(stderr, "Peephole changes (1): %4d\n", s_peep1);
  118.             fprintf(stderr, "Peephole changes (2): %4d\n", s_peep2);
  119.             fprintf(stderr, "Peephole changes (3): %4d\n", s_peep3);
  120.             fprintf(stderr, "Instructions deleted: %4d\n", s_idel);
  121.         }
  122.         if (do_brev)
  123.             fprintf(stderr, "Branch reversals    : %4d\n", s_brev);
  124.         fprintf(stderr, "Branches removed    : %4d\n", s_bdel - s_badd);
  125.     }
  126.  
  127.     /*
  128.      * If we're overwriting the original file, remove the old
  129.      * version, and rename the temp file to the old name.
  130.      */
  131.     if (use_temp) {
  132.         remove(argv[1]);
  133.         rename(TMPFILE, argv[1]);
  134.     }
  135.  
  136.     exit(0);
  137. }
  138.  
  139. dofile()
  140. {
  141.     if (!readline())
  142.         return;
  143.  
  144.     while (dofunc())
  145.         ;
  146. }
  147.